package qinggui.mlz.console.controller.importer;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import qinggui.mlz.console.domain.category.CategoryImportVO;
import qinggui.mlz.module.category.entity.Category;
import qinggui.mlz.module.utils.BaseUtils;
import qinggui.mlz.module.utils.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class ImportCategoryController {
//    /**
//     * poi导入类目表
//     * @param file  文件
//     * @return 响应
//     * @throws IOException 抛出异常
//     */
//    @RequestMapping("/import/category")
//    public Response importCategory(@RequestParam("file") MultipartFile file) throws IOException {
//        if (file == null || file.isEmpty()) {
//            return new Response<>(4004, "文件不能为空");
//        }
//        // 1. 创建工作簿
//        Workbook workbook = new XSSFWorkbook(file.getInputStream());
//        // 2. 获取第一个sheet
//        Sheet sheet = workbook.getSheetAt(0);
//        // 3. 跳过表头，逐行读取
//        List<Category> dataList = new ArrayList<>();
//        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//            Row row = sheet.getRow(i);
//            // 4. 跳过空行
//            if (row == null) continue;
//            Category c = new Category();
//            // 0-父类目  1-名称  2-图片  3-创建时间  4-更新时间
//            String parentVal = BaseUtils.getCellValue(row.getCell(0));
//            c.setParentId(parentVal.isEmpty() ? null : Long.valueOf(parentVal));
//            c.setCategoryName(BaseUtils.getCellValue(row.getCell(1)));
//            c.setCategoryImage(BaseUtils.getCellValue(row.getCell(2)));
//            c.setCreateTime(BaseUtils.date2TimeStamp(BaseUtils.getCellValue(row.getCell(3))));
//            c.setUpdateTime(BaseUtils.date2TimeStamp(BaseUtils.getCellValue(row.getCell(4))));
//            dataList.add(c);
//        }
//        // 释放临时文件
//        workbook.close();
//        return new Response<>(200, dataList);
//    }

    /**
     * easyExcel导入类目表
     * @param file 文件
     * @return 响应
     * @throws IOException 抛出异常
     */
    @RequestMapping("/easyExcelImport/category")
    public Response easyExcelImportCategory(@RequestParam("file") MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            return new Response<>(4004, "文件不能为空");
        }
        List<Category> dataList = new ArrayList<>();
        EasyExcel.read(file.getInputStream(), CategoryImportVO.class, new AnalysisEventListener<CategoryImportVO>() {
            @Override
            public void invoke(CategoryImportVO categoryImportVO, AnalysisContext analysisContext) {
                // 跳过空行
                if (categoryImportVO == null) return;
                // 转实体
                Category c = new Category();
                c.setParentId(categoryImportVO.getParentId());
                c.setCategoryName(categoryImportVO.getCategoryName());
                c.setCategoryImage(categoryImportVO.getCategoryImage());
                c.setCreateTime(categoryImportVO.getCreateTime());
                c.setUpdateTime(categoryImportVO.getUpdateTime());
                dataList.add(c);
                // 要落DB中
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                log.info("读取完成");
            }
        }).sheet().doRead();
        return new Response<>(200, dataList);
    }
}
